//
//  HPSecondTabViewController.m
//  HappyPenguin
//
//  Created by zhuangyihang on 12/30/15.
//  Copyright © 2015 zhuangyihang. All rights reserved.
//

#import "HPSecondTabViewController.h"
#import "HPSecondTabHeaderCell.h"
#import "HPSecondTabContentCell.h"

#import "HPCustomerListViewController.h"
#import "HPFestivalViewController.h"

#import "HPSignInViewController.h"

#import "HPAddCustomerViewController.h"
#import "HPSearchViewController.h"

#import <REFrostedViewController/UIViewController+REFrostedViewController.h>
#import <REFrostedViewController/REFrostedViewController.h>

#import "HPTypes.h"
#import "AppHelper.h"
static const NSInteger kSearchButtonMarginRight = -10;
static const NSInteger kPortraitMarginLeft = 10;
static const NSInteger kButtonFontSize = 12;
static const NSInteger kButtonWidth = 40;
static const NSInteger kPortraitWidth = 40;

static const CGFloat kHeaderHeightRatio = 0.5;


@interface HPSecondTabViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;


//数据
@property (nonatomic, strong) NSArray *cellIconArray;

@end

@implementation HPSecondTabViewController

- (id)init{
    self = [super init];
    if (self) {
        self.tabBarItem.title = @"哥姐";
        self.tabBarItem.image = [UIImage imageNamed:@"Tab_First"];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"Tab_FirstHL"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.cellIconArray = @[@"CellIcon_Festival",@"CellIcon_Feedback",@"CellIcon_Arouse",@"CellIcon_Customer"];
    
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Private
- (void)setupUI{
    [self setupNavigationUI];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.baseContentView addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.baseContentView);
        make.center.equalTo(self.baseContentView);
    }];
}

- (void)setupNavigationUI{
    WS(ws);
    
    UIImageView *portraitImageView = [UIImageView new];
    [self.baseNavigationBarView addSubview:portraitImageView];
    [portraitImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(kPortraitWidth);
        make.bottom.equalTo(self.baseNavigationBarView).offset(-2);
        make.left.mas_equalTo(kPortraitMarginLeft);
    }];
    portraitImageView.backgroundColor = [UIColor whiteColor];
    portraitImageView.layer.cornerRadius = kPortraitWidth/2;
    portraitImageView.clipsToBounds = YES;
    portraitImageView.layer.borderColor = [UIColor colorWithRGB:0x7089a1].CGColor;
    portraitImageView.layer.borderWidth = 1;
    portraitImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [UITapGestureRecognizer bk_recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
        [ws.frostedViewController presentMenuViewController];
    }];
    [portraitImageView addGestureRecognizer:tap];
    
    
    UIButton *search = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baseNavigationBarView addSubview:search];
    [search mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(kButtonWidth);
        make.bottom.equalTo(self.baseNavigationBarView);
        make.right.equalTo(self.baseNavigationBarView).offset(kSearchButtonMarginRight);
    }];
//    [search setTitle:@"搜索" forState:UIControlStateNormal];
    [search setImage:[UIImage imageNamed:@"Button_Search"] forState:UIControlStateNormal];
    search.titleLabel.font = [UIConstants getFont:kButtonFontSize];
    [search bk_addEventHandler:^(id sender) {
        HPSearchViewController *v = [[HPSearchViewController alloc] init];
        [ws.navigationController pushViewController:v animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *sale = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baseNavigationBarView addSubview:sale];
    [sale mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(kButtonWidth);
        make.bottom.equalTo(self.baseNavigationBarView);
        make.right.equalTo(search.mas_left).offset(kSearchButtonMarginRight);
    }];
//    [sale setTitle:@"销售" forState:UIControlStateNormal];
    [sale setImage:[UIImage imageNamed:@"Button_Sale"] forState:UIControlStateNormal];
    sale.titleLabel.font = [UIConstants getFont:kButtonFontSize];
    
    UIButton *newCard = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baseNavigationBarView addSubview:newCard];
    [newCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(kButtonWidth);
        make.bottom.equalTo(self.baseNavigationBarView);
        make.right.equalTo(sale.mas_left).offset(kSearchButtonMarginRight);
    }];
    [newCard setImage:[UIImage imageNamed:@"Button_NewVIP"] forState:UIControlStateNormal];
    newCard.titleLabel.font = [UIConstants getFont:kButtonFontSize];
    
    [search bk_addEventHandler:^(id sender) {
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    [sale bk_addEventHandler:^(id sender) {
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    [newCard bk_addEventHandler:^(id sender) {
        HPAddCustomerViewController *v = [[HPAddCustomerViewController alloc] init];
        [ws.navigationController pushViewController:v animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    return CommonTaskType_Total;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return [self tableView:tableView cellForHeaderRowAtIndexPath:indexPath];
    }
    
    return [self tableView:tableView cellForContentRowAtIndexPath:indexPath];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForHeaderRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"HPSecondTabHeaderCell";
    HPSecondTabHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HPSecondTabHeaderCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForContentRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"HPSecondTabContentCell";
    HPSecondTabContentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HPSecondTabContentCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = CommonTaskTitle[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:self.cellIconArray[indexPath.row]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [cell setBadgeView:1];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return [UIScreen mainScreen].bounds.size.width * kHeaderHeightRatio;
    }else{
        return 50;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==0) {
        
    }else if(indexPath.section==1){
        switch (indexPath.row) {
            case CommonTaskType_Arouse:
                break;
            case CommonTaskType_Customer:
            {
                HPCustomerListViewController *v = [HPCustomerListViewController new];
                [self.navigationController pushViewController:v animated:YES];
            }
                break;
            case CommonTaskType_Feedback:
            {
                
            }
                break;
            case CommonTaskType_Festival:
            {
                HPFestivalViewController *v = [HPFestivalViewController new];
                [self.navigationController pushViewController:v animated:YES];
            }
                break;
            default:
                break;
        }
    }
}


@end
